<?php
//Fichiers pour générer des graphiques
include("../../library/jpgraph-3.5.0b1/src/jpgraph.php"); //Librairie générale
include("../../library/jpgraph-3.5.0b1/src/jpgraph_line.php"); //Librairie pour les graphiques en ligne
include("../../library/jpgraph-3.5.0b1/src/jpgraph_table.php"); //Librairie pour les tableaux de données

//Fichiers des algos
include('../../Objects/House.php');
//Wall
include('../../Algos/Wall/deperditionWalls.php');
include('../../Objects/Wall.php');
//Window
include('../../Algos/Window/deperditionWindows.php');
include('../../Objects/Window.php');
include('../../Algos/Window/deperditionZenithalWindows.php');
include('../../Objects/ZenithalWindow.php');
//Roof
include('../../Algos/Roof/deperditionRoof.php');
include('../../Objects/Roof.php');
//Ceiling
include('../../Algos/Ceiling/deperditionCeiling.php');
include('../../Objects/Ceiling.php');
//Floor
include('../../Algos/Floor/deperditionFloor.php');
include('../../Objects/Floor.php');
//Heating
include('../../Algos/Heating/consoHeating.php');
include('../../Objects/HeatPump.php');
include('../../Objects/OilBoiler.php');
include('../../Objects/WoodPelletBoiler.php');
//Total
include('../../Algos/totalDeperdition.php');
include('../../Algos/deperditionTransmission.php');
include('../../Algos/deperditionByVentilation.php');

try {
    $Mysql = new DBAccess('153.109.141.81:3306', 'savenrj', 'root', '');

    /* RECUPERATION DES DONNEES */
    $landscape = $_POST['landscape']; //Id Landscape

    $adresse = $_POST['adresse'];
    $ville = $_POST['ville'];
    $idVille = 77; //TODO: récupérer ID station météo

    $elevation = 500;

    $houseShape = $_POST['shape']; //Id house shape
    $nbreMur = 4; //Pour démo, forme carré ou rectangle avec 4 murs !!
    $roofShape = $_POST['roofShape']; //Id roof shape
    if($roofShape == 'A'){
        $roofShape = 1;
    }
    if($roofShape == 'B1'){
        $roofShape = 2;
    }
    if($roofShape == 'B2'){
        $roofShape = 3;
    }
    if($roofShape == 'B3'){
        $roofShape = 4;
    }
    if($roofShape == 'C'){
        $roofShape = 5;
    }
    if($roofShape == 'D'){
        $roofShape = 6;
    }
    if($roofShape == 'E'){
        $roofShape = 7;
    }

    $yearHouse = $_POST['yearHouse'];
    $yearHouse2012 = '2012';
    $floors = $_POST['floorsOccupied'];
    $balcony = $_POST['balcony']; //Boolean
    $eave = $_POST['eaves']; //Boolean

    $ridge = $_POST['inputRidge'];
    $h = $_POST['inputH'];
    $m1 = $_POST['inputM1'];
    $m2 = $_POST['inputM2'];
    $m3 = $_POST['inputM3'];
    $m4 = $_POST['inputM4'];
    
    $roofHeight = $ridge-$h;

    $area = $_POST['area'];

    $temp = $_POST['temperatureIndoor'];
    $situation = $_POST['situation']; //
    $orientationHouse = $_POST['orientation']; //Pour savoir quel mur est mitoyen
    $heatingHouse = $_POST['heating'];

    $yearWalls = $_POST['yearChangedWall']; //TODO: Tester si l'année est différente de celle de la maison
    $orientationWallA = $_POST['selectOrientation']; //Selon l'orientation du mur A, on déduit l'orientation des autres murs
    $orientationWallB = null;
    $orientationWallC = null;
    $orientationWallD = null;
    if ($orientationWallA == "Nord") {
        $orientationWallA = 1;
        $orientationWallB = 2;
        $orientationWallC = 3;
        $orientationWallD = 4;
    }
    if ($orientationWallA == "Est") {
        $orientationWallA = 2;
        $orientationWallB = 3;
        $orientationWallC = 4;
        $orientationWallD = 1;
    }
    if ($orientationWallA == "Sud") {
        $orientationWallA = 3;
        $orientationWallB = 4;
        $orientationWallC = 1;
        $orientationWallD = 2;
    }
    if ($orientationWallA == "Ouest") {
        $orientationWallA = 4;
        $orientationWallB = 1;
        $orientationWallC = 2;
        $orientationWallD = 3;
    }

    $wallA = $_POST['areaWallA'];
    $wallB = $_POST['areaWallB'];
    $wallC = $_POST['areaWallC'];
    $wallD = $_POST['areaWallD'];

    $areaRoof = $_POST['areaRoof'];
    $yearRoof = $_POST['yearChangedRoof']; //TODO: Tester si l'année est différente de celle de la maison

    $areaFloor = $_POST['areaFloor'];
    $yearFloor = $_POST['yearChangedFloor']; //TODO: Tester si l'année est différente de celle de la maison

    $areaCeiling = $_POST['areaCeiling'];
    $yearCeiling = $_POST['yearChangedCeiling']; //TODO: Tester si l'année est différente de celle de la maison

    $windowType = $_POST['windowType']; //Id window type
    $yearWindow = $_POST['yearChangedWindow']; //TODO: Tester si l'année est différente de celle de la maison
    $roofLight = $_POST['roofLightNumber'];
    $blinds = $_POST['blinds']; //Boolean
//En pourcentage
    $areaWindowNorth = $_POST['areaWindowNorth'];
    $areaWindowEast = $_POST['areaWindowEast'];
    $areaWindowSouth = $_POST['areaWindowSouth'];
    $areaWindowWest = $_POST['areaWindowWest'];
//En m2
    $measureWindowNorth = $_POST['measureWindowNorth'];
    $measureWindowEast = $_POST['measureWindowEast'];
    $measureWindowSouth = $_POST['measureWindowSouth'];
    $measureWindowWest = $_POST['measureWindowWest'];

    $heatingFacilites = $_POST['selectHeating'];
    $yearHeating = $_POST['yearChangedHeating']; //TODO: Tester si l'année est différente de celle de la maison
    $consommationHeating = $_POST['consommationChauffage'];
    $uniteH = $_POST['uniteChauffage'];
    $consommationHeatingCHF = $_POST['consommationChauffageCHF'];

    $hotWaterFacilites = $_POST['selectHotWater'];
    $yearHotWater = $_POST['yearChangedHotWater']; //TODO: Tester si l'année est différente de celle de la maison
    $consommationHotWater = $_POST['consommationEauChaude'];
    $uniteHW = $_POST['uniteEauChaude'];
    $consommationHotWaterCHF = $_POST['consommationEauChaudeCHF'];

    /* CONSTRUCTION DES OBJETS */
    $oWindows = array();
    $oWindows[0] = new Window(1, 1, 2, $blinds, 2, 2.2, 0.22, 0.1, 0.15, '', '', '', '');
    $oWindows[1] = new Window(1, 1, 2, $blinds, 2, 2.2, 0.22, 0.1, 0.15, '', '', '', '');

    $oZenithalWindow = array();
    $oZenithalWindow[0] = new ZenithalWindow();

    $oWalls = array();
    
    $oWalls[0] = new Wall('M1', $orientationWallA, 1, $wallA, '', $oWindows, $yearWalls);
    $oWalls[1] = new Wall('M2', $orientationWallB, 1, $wallB, '', $oWindows, $yearWalls);
    $oWalls[2] = new Wall('M3', $orientationWallC, 1, $wallC, '', $oWindows, $yearWalls);
    $oWalls[3] = new Wall('M4', $orientationWallD, 1, $wallD, '', $oWindows, $yearWalls);

    $oRoof = new Roof($roofShape, $yearHouse, '', $roofHeight);

    $oFloor = new Floor(1);

    $oCeiling = new Ceiling($yearCeiling, '');

//    $oHeating;
//    if($heatingFacilites == 1){
//        
//    }
//    $oHeating = new HeatPump($billValue, $unit, $kwhPrice, $pac, $isECSOnly);
//    $oHeating = new OilBoiler($billValue, $unit, $literPrice, $kwhPrice, $isECSOnly);
//    $oHeating = new SoapstoneBoiler($billValue);
//    $oHeating = new WoodPelletBoiler($billValue, $isECSOnly, $material);

    if ($houseShape == 1) { //Carré
        $oHouse = new House(4, $landscape, null, $houseShape, $m1, '', '', '', '', '', '', $idVille, $temp, $floors, $yearHouse, $oWalls, $oFloor, $oCeiling, $oRoof, $oZenithalWindow, '', $balcony, $eave);
        $oHouse2012 = new House(4, $landscape, null, $houseShape, $m1, '', '', '', '', '', '', $idVille, $temp, $floors, $yearHouse2012, $oWalls, $oFloor, $oCeiling, $oRoof, $oZenithalWindow, '', $balcony, $eave);
    }
    if ($houseShape == 2) { //Rectangle
        $oHouse = new House(4, $landscape, null, $houseShape, $m1, $m2, '', '', '', '', '', $idVille, $temp, $floors, $yearHouse, $oWalls, $oFloor, $oCeiling, $oRoof, $oZenithalWindow, '', $balcony, $eave);
        $oHouse2012 = new House(4, $landscape, null, $houseShape, $m1, $m2, '', '', '', '', '', $idVille, $temp, $floors, $yearHouse2012, $oWalls, $oFloor, $oCeiling, $oRoof, $oZenithalWindow, '', $balcony, $eave);
    }

//Appel des algos
    $algoMur = new AlgoWall();
    $algoFenetre = new AlgoWindows();
    $algoToit = new AlgoRoof();
    $algoSol = new AlgoFloor();
    $algoPlafond = new AlgoCeiling();
    $algoTotale = new totalDeperdition();

    //Déperditions
    $deperditionMur = $algoMur->getTotalDeperdition($oHouse, $oWalls);
    $deperditionFenetre = $algoFenetre->getTotalDepertitionWindows($oHouse);
    $deperditionToit = $algoToit->getTotalDepertitionRoof($oHouse);
    $deperditionSol = $algoSol->getTotalDeperditionFloor($oHouse);
    $deperditionPlafond = $algoPlafond->getTotalDeperditionCeiling($oHouse);
    $deperditionTotale = $algoTotale->getTotalDeperdition($oHouse, $elevation);
    //Déperditions 2012
    $deperditionMur2012 = $algoMur->getTotalDeperdition($oHouse2012, $oWalls);
    $deperditionFenetre2012 = $algoFenetre->getTotalDepertitionWindows($oHouse2012);
    $deperditionToit2012 = $algoToit->getTotalDepertitionRoof($oHouse2012);
    $deperditionSol2012 = $algoSol->getTotalDeperditionFloor($oHouse2012);
    $deperditionPlafond2012 = $algoPlafond->getTotalDeperditionCeiling($oHouse2012);
    $deperditionTotale2012 = $algoTotale->getTotalDeperdition($oHouse2012, $elevation);
    
    /* DONNEES POUR GRAPHIQUES */
    $labelsDeperdition = array('Murs', 'Fenetre', 'Toit', 'Sol', 'Plafond', 'Total');
    $dataDeperditionHouse = array($deperditionMur, $deperditionFenetre, $deperditionToit, $deperditionSol, $deperditionPlafond, $deperditionTotale);//Maison à comparer
    $dataDeperditionHouse2012 = array($deperditionMur2012, $deperditionFenetre2012, $deperditionToit2012, $deperditionSol2012, $deperditionPlafond2012, $deperditionTotale2012);//Maison 2012

    /* GRAPHIQUES */
    $graphDeperdition = new Graph(700, 700);
    $graphDeperdition->img->SetMargin(60, 30, 50, 140);
    $graphDeperdition->SetScale("textlin");
    
    //Afficher la grille de l'axe des ordonnées
    $graphDeperdition->ygrid->Show();
    $graphDeperdition->ygrid->Setcolor('lightgray');

    //Afficher la grille de l'axe des abscisses
    $graphDeperdition->xgrid->Show();
    $graphDeperdition->xgrid->Setcolor('lightgray');
    
    ///Créer une courbe
    $courbeDeperdition = new LinePlot($dataDeperditionHouse);
    $courbeDeperdition2012 = new LinePlot($dataDeperditionHouse2012);
    
    // Points de la courbe
    $courbeDeperdition->mark->SetType(MARK_FILLEDCIRCLE);
    $courbeDeperdition->mark->SetFillColor('gray');
    $courbeDeperdition->mark->SetWidth(2);
    
    $courbeDeperdition2012->mark->SetType(MARK_FILLEDCIRCLE);
    $courbeDeperdition2012->mark->SetFillColor('gray');
    $courbeDeperdition2012->mark->SetWidth(2);
    
    //Couleur de la courbe
    $courbeDeperdition->SetColor('lightblue');
    $courbeDeperdition2012->SetColor('aqua');

    $courbeDeperdition->SetCenter(); //Permet de centrer le graphique
    
    //Tableaux
    $tableDeperdition = new GTextTable();
    $dataDeperditionHouse = array($labelsDeperdition, $dataDeperditionHouse, $dataDeperditionHouse2012);
    $tableDeperdition->Set($dataDeperditionHouse);
    $tableDeperdition->SetPos(100, 600);
    $tableDeperdition->SetRowFont(0, FF_ARIAL, FS_BOLD);
    
    //Paramétrage des axes
    $graphDeperdition->yaxis->title->Set('MJ/m2');
    $graphDeperdition->yaxis->title->SetFont(FF_FONT1, FS_BOLD);
    $graphDeperdition->yaxis->title->SetMargin(25);
    
    $graphDeperdition->xaxis->SetTickLabels($labelsDeperdition);
    $graphDeperdition->xaxis->title->SetFont(FF_FONT1, FS_BOLD);

    //Ajouter les courbes et le tableau des données au conteneur
    $graphDeperdition->Add($courbeDeperdition);
    $graphDeperdition->Add($courbeDeperdition2012);
    $graphDeperdition->Add($tableDeperdition);

    //Affichage du graphique
    $graphDeperdition->Stroke("../../images/reportPDF/graphDeperdition.png"); //Enregistrer le graphique au format image
    
}
catch (MySQLExeption $e) {
    echo $e->RetourneErreur();
}
?>